Skip to content

Method: doesLanguageMatch(OWLLiteral, Assertion)

1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.ontodriver.owlapi.util;
14:
15: import cz.cvut.kbss.jopa.owlapi.DatatypeTransformer;
16: import cz.cvut.kbss.ontodriver.model.Assertion;
17: import cz.cvut.kbss.ontodriver.model.NamedResource;
18: import cz.cvut.kbss.ontodriver.owlapi.config.Constants;
19: import org.semanticweb.owlapi.model.IRI;
20: import org.semanticweb.owlapi.model.OWLDataFactory;
21: import org.semanticweb.owlapi.model.OWLLiteral;
22: import org.semanticweb.owlapi.model.OWLNamedIndividual;
23:
24: import java.net.URI;
25: import java.net.URL;
26:
27: /**
28: * Utility methods for the OWLAPI driver.
29: */
30: public class OwlapiUtils {
31:
32: private OwlapiUtils() {
33: throw new AssertionError("Can't create instance.");
34: }
35:
36: /**
37: * Creates OWLLiteral from the specified Java instance.
38: *
39: * @param value The value to transform
40: * @param lang Ontology language
41: * @return OWLLiteral representing the value
42: * @throws IllegalArgumentException If {@code value} is of unsupported type
43: */
44: public static OWLLiteral createOWLLiteralFromValue(Object value, String lang) {
45: return DatatypeTransformer.transform(value, lang);
46: }
47:
48: /**
49: * Transforms OWLLiteral to a plain Java object (boxed primitive or date/time).
50: *
51: * @param literal The literal to transform
52: * @return Transformed value
53: * @throws IllegalArgumentException If the literal is of unsupported type
54: */
55: public static Object owlLiteralToValue(final OWLLiteral literal) {
56: return DatatypeTransformer.transform(literal);
57: }
58:
59: /**
60: * Checks whether the specified literal matches to the language of the specified assertion.
61: * <p>
62: * If the literal is not a string, it automatically matches. If it is a string, it matches if assertion language is
63: * not specified, it is without language tag or if the language tag matches the specified assertion language.
64: *
65: * @param literal Literal to check
66: * @param assertion Assertion with language specification (possibly empty)
67: * @return {@code true} if the literal matches the assertion language, {@code false} otherwise
68: */
69: public static boolean doesLanguageMatch(OWLLiteral literal, Assertion assertion) {
70:• assert literal != null;
71:• assert assertion != null;
72:
73:• return !assertion.hasLanguage() || literal.getLang().isEmpty() ||
74:• literal.getLang().equals(assertion.getLanguage());
75: }
76:
77: /**
78: * Retrieves language from the specified assertion.
79: * <p>
80: * If the assertion does not specify language, {@link Constants#DEFAULT_LANGUAGE} is returned.
81: *
82: * @param assertion Assertion to get language for
83: * @return Assertion language or default language
84: */
85: public static String getAssertionLanguage(Assertion assertion) {
86: return assertion.hasLanguage() ? assertion.getLanguage() : Constants.DEFAULT_LANGUAGE;
87: }
88:
89: /**
90: * Gets OWLNamedIndividual for the specified named resource.
91: *
92: * @param subject Named resource to transform to individual
93: * @param dataFactory OWL data factory
94: * @return OWLNamedIndividual
95: */
96: public static OWLNamedIndividual getIndividual(NamedResource subject, OWLDataFactory dataFactory) {
97: return dataFactory.getOWLNamedIndividual(IRI.create(subject.getIdentifier()));
98: }
99:
100: /**
101: * Checks whether the specified value is a valid IRI.
102: * <p>
103: * Only absolute IRIs are accepted.
104: *
105: * @param value The value to check
106: * @return {@code true} for instances of {@link NamedResource}, {@link URI}, {@link URL} or {@link IRI} and for
107: * Strings parseable by {@link URI#create(String)}.
108: */
109: public static boolean isIndividualIri(Object value) {
110: if (value instanceof NamedResource || value instanceof URI || value instanceof URL || value instanceof IRI) {
111: return true;
112: }
113: if (!(value instanceof String)) {
114: return false;
115: }
116: try {
117: final IRI iri = IRI.create(value.toString());
118: return iri.isAbsolute();
119: } catch (IllegalArgumentException e) {
120: return false;
121: }
122: }
123: }